<?php

namespace app\service;

use app\model\Article;


class ArticleService extends BaseService
{

    protected function setModel(): string
    {
        return Article::class;
    }

    public function saveAll($params)
    {

        $model = $this->getModel();
        if (!empty($params["id"])) {
            $model = $this->get($params["id"]);
            $model->categories()->detach();
        }
        $model->save($params);
        return $model->categories()->save($params["category_id"]);
    }

    public function getList($where = [], $field = "*", $with = [])
    {
        $this->getPageAndSize();
        $list = $this->getModel()->when(!empty($with), function ($query) use ($with, $where) {
            $query->with($with)->when(!empty($where["category_id"]), function ($query) use ($where) {
                $query->whereIn('id', function ($query) use ($where) {
                    $query->name('article_category')
                        ->whereIn('category_id', $where["category_id"])
                        ->field('article_id')
                        ->select()
                        ->toArray();
                });
            });;
        })->when(!empty($where["title"]), function ($query) use ($where) {
            $query->whereLike("title", sprintf("%%s%", $where["title"]));
        })->when(!empty($where["status"]), function ($query) use ($where) {
            $query->where("status", $where["status"]);
        })->order($this->getOrder())->field($field)->when($this->page && $this->pageSize, function ($query) {
            $query->page($this->page, $this->pageSize);
        })->select()->toArray();
        $count = $this->getModel()->when(!empty($with), function ($query) use ($with, $where) {
            $query->with($with)->when(!empty($where["category_id"]), function ($query) use ($where) {
                $query->whereIn('id', function ($query) use ($where) {
                    $query->name('article_category')->whereIn('category_id', $where["category_id"])->field('article_id')->select();;
                });
            });
        })->when(!empty($where["title"]), function ($query) use ($where) {
            $query->whereLike("title", sprintf("%%s%", $where["title"]));
        })->when(!empty($where["status"]), function ($query) use ($where) {
            $query->where("status", $where["status"]);
        })->count();
        return ["rows" => [
            "total" => $count,
            "list" => $list,
        ]];
    }

    /**
     * 不需要total
     * @param $where
     * @param $field
     * @param array $with
     * @return mixed
     */
    public function getListLimitWith(array $where=[], string $field = "*", array $with = [],int $limit=3,$order = "id desc"): array
    {
        $list = $this->getModel()->when(!empty($with), function ($query) use ($with, $where) {
            $query->with($with)->when(!empty($where["category_id"]), function ($query) use ($where) {
                $query->whereIn('id', function ($query) use ($where) {
                    $query->name('article_category')
                        ->whereIn('category_id', $where["category_id"])
                        ->field('article_id')
                        ->select()
                        ->toArray();
                });
            });
        })->when(!empty($where["status"]), function ($query) use ($where) {
            $query->where("status", $where["status"]);
        })->order($order)->field($field)->limit($limit)->select()->toArray();
        return $list;
    }
    public function getListWithOutCount($where = [], $field = "*", array $with = [])
    {
        $this->getPageAndSize();
        $list = $this->getModel()->when(!empty($with), function ($query) use ($with, $where) {
            $query->with($with)->when(!empty($where["category_id"]), function ($query) use ($where) {
                $query->whereIn('id', function ($query) use ($where) {
                    $query->name('article_category')
                        ->whereIn('category_id', $where["category_id"])
                        ->field('article_id')
                        ->select()
                        ->toArray();
                });
            });;
        })->when(!empty($where["title"]), function ($query) use ($where) {
            $query->whereLike("title", sprintf("%%s%", $where["title"]));
        })->when(!empty($where["status"]), function ($query) use ($where) {
            $query->where("status", $where["status"]);
        })->order($this->getOrder())->field($field)->when($this->page && $this->pageSize, function ($query) {
            $query->page($this->page, $this->pageSize);
        })->select()->toArray();
        return $list;

    }
}